<?php

class SJB_Admin_Classifieds_FlaggedListings extends SJB_Function
{
	public function isAccessible()
	{
		$this->setPermissionLabel('manage_flagged_listings');
		return parent::isAccessible();
	}

	public function execute()
	{
		$tp = SJB_System::getTemplateProcessor();
		$errors = array();

		$listingTypeID = SJB_Request::getVar('listing_type_id');
		$listingTypeSID = SJB_Request::getVar('listing_type');

		if ($listingTypeID !== null) {
			$listingTypeSID = SJB_ListingTypeManager::getListingTypeSIDByID($listingTypeID);
		}

		// SET PAGINATION AND SORTING VALUES
		$page = SJB_Request::getVar('page', 1);
		$perPage = SJB_Request::getVar('per_page', 20);

		// SORTING
		$sortingField = SJB_Request::getVar('sorting_field');
		$sortingOrder = SJB_Request::getVar('sorting_order');
		$restore = SJB_Request::getVar('restore', false);

		// FILTERS
		$filters = array();
		$filters['title'] = SJB_Request::getVar('filter_title');
		$filters['username'] = SJB_Request::getVar('filter_user');
		$filters['flag'] = SJB_Request::getVar('filter_flag');

		// check session for pagination settings
		$sessionFlaggedSettings = !is_null(SJB_Session::getValue('flagged_settings')) ? SJB_Session::getValue('flagged_settings') : false;
		if ($sessionFlaggedSettings !== false) {
			// if we have new value
			if (SJB_Request::getVar('per_page', false)) {
				$sessionFlaggedSettings['per_page'] = $perPage;
				SJB_Session::setValue('flagged_settings', $sessionFlaggedSettings);
			} elseif (!empty($sessionFlaggedSettings['per_page'])) {
				$perPage = $sessionFlaggedSettings['per_page'];
			}

			if (!$restore) {
				SJB_Session::setValue('flagged_settings', array(
					'sorting_field' => $sortingField,
					'sorting_order' => $sortingOrder,
					'listing_type_sid' => $listingTypeSID,
					'filters' => $filters
				));
			} else {
				if (!$sortingField) {
					$sortingField = $sessionFlaggedSettings['sorting_field'];
				} else {
					$sessionFlaggedSettings['sorting_field'] = $sortingField;
					SJB_Session::setValue('flagged_settings', $sessionFlaggedSettings);
				}
				if (!$sortingOrder) {
					$sortingOrder = $sessionFlaggedSettings['sorting_order'];
				} else {
					$sessionFlaggedSettings['sorting_order'] = $sortingOrder;
					SJB_Session::setValue('flagged_settings', $sessionFlaggedSettings);
				}

				if (!$listingTypeSID && !empty($sessionFlaggedSettings['listing_type_sid'])) {
					$listingTypeSID = $sessionFlaggedSettings['listing_type_sid'];
				}

				$filters = $sessionFlaggedSettings['filters'];
			}
		} else {
			SJB_Session::setValue('flagged_settings', array(
				'per_page' => $perPage,
				'sorting_field' => $sortingField,
				'sorting_order' => $sortingOrder,
				'listing_type_sid' => $listingTypeSID,
				'filters' => $filters
			));
		}

		// DEFAULT SORTING
		if (!$sortingField) {
			$sortingField = 'date';
			$sortingOrder = 'DESC';
		}

		// resolve flag to it text value for search
		$filterFlag = $filters['flag'];
		if (!empty($filterFlag) && is_numeric($filterFlag)) {
			$result = SJB_DB::query('SELECT * FROM `flag_listing_settings` WHERE `sid` = ?n LIMIT 1', $filterFlag);
			if (!empty($result)) {
				$filters['flag_reason'] = $result[0]['value'];
			}
		}

		//////////////////////  ACTIONS
		$action = SJB_Request::getVar('action_name');
		$flagSIDs = SJB_Request::getVar('flagged');

		if (!empty($flagSIDs)) {
			switch ($action) {
				case 'remove':
					foreach ($flagSIDs as $sid => $val)
						SJB_ListingManager::removeFlagBySID($sid);
					break;
				case 'deactivate':
					foreach ($flagSIDs as $sid => $val)
						SJB_ListingManager::deactivateListingByFlagSID($sid);
					break;
				case 'delete':
					foreach ($flagSIDs as $sid => $val)
						SJB_ListingManager::deleteListingByFlagSID($sid);
					break;
			}
		}

		//////////////////////// OUTPUT
		$allListingTypes = SJB_ListingTypeManager::getAllListingTypesInfo();

		$allFlags = SJB_ListingManager::getAllFlags();
		$totalPages = SJB_ListingManager::getFlaggedTotalPagesNum($listingTypeSID, $perPage, $filters);

		$totalFlagsNum = SJB_ListingManager::getFlagsNumberByListingTypeSID($listingTypeSID, $filters);
		$flaggedListings = SJB_ListingManager::getFlaggedListings($listingTypeSID, $page, $perPage, $sortingField, $sortingOrder, $filters);

		foreach ($flaggedListings as $key => $val) {
			$listingInfo = SJB_ListingManager::getListingInfoBySID($val['listing_sid']);
			$listingUser = SJB_UserManager::getUserInfoBySID($listingInfo['user_sid']);
			$flaggedUser = SJB_UserManager::getUserInfoBySID($val['user_sid']);
			$flaggedListings[$key]['listing_info'] = $listingInfo;
			$flaggedListings[$key]['user_info'] = $listingUser;
			$flaggedListings[$key]['flagged_user'] = $flaggedUser;
		}

		$tp->assign('listing_types', $allListingTypes);
		$tp->assign('listings', $flaggedListings);
		$tp->assign('listing_type_sid', $listingTypeSID);
		$tp->assign('all_flags', $allFlags);

		$tp->assign('total_flags_number', $totalFlagsNum);

		// pagination
		$tp->assign('pages', $totalPages);
		$tp->assign('current_page', $page);
		$tp->assign('per_page', $perPage);

		// sorting fields to filter form
		$tp->assign('sorting_field', $sortingField);
		$tp->assign('sorting_order', $sortingOrder);
		$tp->assign('filters', $filters);

		$tp->display('flagged_listings.tpl');
	}
}
